package com.zline.foodraise.view;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.SparseArray;
import android.view.View;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.zline.foodraise.R;
import com.zline.foodraise.adapter.CommentListItemPicsAdapter;
import com.zline.foodraise.constant.Constant;
import com.zline.foodraise.entity.CommentBean;
import com.zline.foodraise.entity.CustomerUserBean;
import com.zline.foodraise.entity.PictureBean;
import com.zline.foodraise.listener.RecyclerItemClicked;
import com.zline.foodraise.util.ImageUtil;
import com.zline.foodraise.util.StringUtils;
import com.zline.foodraise.util.TimeUtil;

import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EViewGroup;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.StringRes;

import java.util.List;

/**
 * Created by heshantao on 15/8/1.
 * 评论列表Item
 */
@EViewGroup(R.layout.comment_list_item)
public class CommentListItem extends RelativeLayout {
    private static final String LOG_TAG = CommentListItem.class.getSimpleName();
    RecyclerItemClicked itemClicked;
    @ViewById(R.id.comment_list_item_person_icon_circleIv)
    CircleImageView iv;
    @ViewById(R.id.comment_list_item_vip)
    ImageView vip_iv;
    @ViewById(R.id.nickName_tv)
    TextView nickeName_tv;
    @ViewById(R.id.comment_tv)
    TextView comment_tv;
    @ViewById(R.id.comment_time_tv)
    TextView time_tv;
    @ViewById(R.id.comment_pics_recyclerView)
    RecyclerView picRecyclerView;
    //评论点赞数
    @ViewById(R.id.comment_support_num_tv)
    TextView support_num_tv;
    @ViewById(R.id.comment_support_icon_iv)
    ImageView support_icon_iv;
    @ViewById(R.id.line0)
    View line;
    @Bean
    CommentListItemPicsAdapter picsAdapter;
    SparseArray<String> picsSparse;
    ImageLoader imageLoader;
    String picUrl;
    int identificateType;
    int position;
    @StringRes(R.string.reply)
    String reply;
    Context cxt;
    @ViewById(R.id.rb_score)
    RatingBar ratingBar;
    //用来标示普通评论列表，热门评论列表
    int flag;
    @StringRes(R.string.today)
    String today;
    @Bean
    TimeUtil timeUtil;

    public CommentListItem(Context context) {
        super(context);
        this.cxt = context;
    }

    public void bind(CommentBean commentBean, int position, int flag,DisplayImageOptions options) {
        this.position = position;
        this.flag = flag;
        imageLoader = ImageLoader.getInstance();
        CustomerUserBean customerUserBean = commentBean.getUserBean();
        if (customerUserBean == null) return;

        picUrl = customerUserBean.getAvatarPicUrl();
        imageLoader.displayImage(picUrl, iv, options);
        identificateType=customerUserBean.getIdentificateType();
        if(identificateType==1){
            vip_iv.setVisibility(VISIBLE);
        }else {
            vip_iv.setVisibility(GONE);
        }
        String name = customerUserBean.getNickName();
        int name_length = name.length();
        String replyUserName = commentBean.getReplyNickName();
        if (StringUtils.isBlank(replyUserName)) {
            nickeName_tv.setText(name);
        } else {
            String title = name + reply + replyUserName;
            SpannableString spannableString = new SpannableString(title);
            spannableString.setSpan(new ForegroundColorSpan(Color.BLACK)
                    , name_length, name_length + 2, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
            nickeName_tv.setText(spannableString);
        }
        String content = commentBean.getContent();
        comment_tv.setText(content);
        long time = commentBean.getCreateTimeStamp();
        String createTime = formatData(time);
        time_tv.setText(createTime);
        List<PictureBean> pictureBeanList = commentBean.getPictureBeanList();
        picsSparse = new SparseArray<String>();
        for (int i = 0; i < pictureBeanList.size(); i++) {
            PictureBean pictureBean = pictureBeanList.get(i);
            picsSparse.put(i, pictureBean.getPictureUrl());
        }
        setPicRecyclerView();

        int supportNum = commentBean.getCommentPraiseCount();
        showOrHideSupportNum(supportNum);
        int isSupport = commentBean.getIsPraise();
        changeSupportStatus(isSupport);
        if(flag==Constant.HOT_COMMENT_FLAG){
            line.setVisibility(View.VISIBLE);
        }
    }


    //改变点赞状态
    void changeSupportStatus(int isSupport) {
        if (isSupport == 1) {
            support_icon_iv.setImageResource(R.mipmap.comment_support);
        } else {
            support_icon_iv.setImageResource(R.mipmap.comment_unsupport);
        }
    }

    void showOrHideSupportNum(int supportNum) {
        if (supportNum > 0) {
            support_num_tv.setText(supportNum + "");
            support_num_tv.setVisibility(View.VISIBLE);
        } else {
            support_num_tv.setVisibility(View.GONE);
        }
    }


    //设置recyclerView
    void setPicRecyclerView() {
        picRecyclerView.setOverScrollMode(RecyclerView.OVER_SCROLL_NEVER);//去除阴影
        MyGridLayoutManager layoutManager = new MyGridLayoutManager(cxt, 3);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        picRecyclerView.setLayoutManager(layoutManager);
        picsAdapter.setAdapterData(picsSparse);
        picsAdapter.setRecycleItemClickListener(itemClicked);
        picsAdapter.setRecycleFlag(flag);
        picsAdapter.setParentPostion(position);
        picRecyclerView.setAdapter(picsAdapter);
        picRecyclerView.setItemAnimator(new DefaultItemAnimator());//设置动画
    }

    //将时间戳转化为日期格式
    String formatData(long timeStamp) {
        String result = "";
        if (timeUtil.isToday(timeStamp)) {
            result = today + " " + timeUtil.formatHourAndMin(timeStamp);
        } else {
            result = timeUtil.formatData2(timeStamp);
        }
        return result;
    }


    public void setRecyclerItemClick(RecyclerItemClicked clicked) {
        this.itemClicked = clicked;
    }

    @Click({R.id.comment_list_item_rl, R.id.comment_support_icon_iv})
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.comment_list_item_rl:
                if (flag == Constant.COMMENT_FLAG) {
                    itemClicked.onRecyclerItemClicked(Constant.COMMENT_LIST_ITEM_FLAG, position);
                } else if (flag == Constant.HOT_COMMENT_FLAG) {
                    itemClicked.onRecyclerItemClicked(Constant.HOT_COMMENT_LIST_ITEM_FLAG, position);
                }
                break;
            case R.id.comment_support_icon_iv:
                itemClicked.onRecyclerItemClicked(flag, position);
                break;
            default:
                break;
        }

    }
}
